<?php
//模块插件类
//插件功能类，由系统调用，实现具体功能。
defined('BY_JYA') or exit('error');
require 'class/sig_master/usersig.php';
class core_jy_cmslive extends plugin{
	public function getLiveInfo($url = NULL, $type = 'auto')
	{
		if (empty($url)) {
			exi('视频地址为空','error','/index/');
		}
		$liveList = array('panda' => '熊猫直播', 'douyu' => '斗鱼直播', 'huajiao' => '花椒直播', 'yizhibo' => '一直播', 'inke' => '映客直播', 'qlive' => '青果直播', 'ys7' => '萤石直播');
		if ($type == 'auto') {
			foreach ($liveList as $key => $val) {
				if (strpos($url, $key)) {
					if (($key == 'huajiao') && strpos($url, 'shuidi')) {
						continue;
					}
					$type = $key;
					break;
				}
			}
			if ($type == 'auto') {
				exi('未自动识别到视频来源','error','/index/');
			}
		}
		$resultArr = array();
		switch ($type) {
		case 'panda':
			preg_match('/.*panda.tv\\/(\\d+)/is', $url, $matchs);
			if (empty($matchs)) {
			exi('视频地址参数错误或所选来源错误','error','/index/');	
			}
			$roomid = $matchs[1];
			if (strpos($matchs[0], 'xingyan') !== false) {
				$apiResult = cfc('http')->ihttp_get('http://m.api.xingyan.panda.tv/room/baseinfo?xid=' . $roomid);
			}
			else {
				$apiResult = cfc('http')->ihttp_get('https://room.api.m.panda.tv/index.php?callback=&method=room.shareapi&roomid=' . $roomid);
			}
			$apiResult = json_decode($apiResult['content'], true);
			if (!empty($apiResult['errno'])) {
			exi('获取房间信息失败','error','/index/');	
			}
			if (strpos($matchs[0], 'xingyan') !== false) {
				$resultArr = array('status' => $apiResult['data']['roominfo']['playstatus'] == 1 ? 1 : 0, 'poster' => $apiResult['data']['roominfo']['photo'], 'hls_url' => $apiResult['data']['videoinfo']['hlsurl']);
			}
			else {
				$resultArr = array('status' => $apiResult['data']['roominfo']['status'] == 2 ? 1 : 0, 'poster' => $apiResult['data']['roominfo']['pictures']['img'], 'hls_url' => $apiResult['data']['videoinfo']['address']);
			}
			break;
		case 'douyu':
			preg_match('/.*douyu.com\\/(\\d+)/is', $url, $matchs);
			if (empty($matchs)) {
				exi('视频地址参数错误或所选来源错误','error','/index/');	
			}
			$roomid = $matchs[1];
			$apiResult = cfc('http')->ihttp_get('https://m.douyu.com/html5/live?roomId=' . $roomid);
			$apiResult = json_decode($apiResult['content'], true);
			if (empty($apiResult['data'])) {
				exi('获取房间信息失败','error','/index/');	
			}
			if (!empty($apiResult['error'])) {
			exi($apiResult['data'],'error','/index/');	
			}
			$resultArr = array('status' => $apiResult['data']['error'] == 0 ? 1 : 0, 'poster' => '', 'hls_url' => $apiResult['data']['hls_url']);
			if (!empty($resultArr['status'])) {
				$html = $apiResult = cfc('http')->ihttp_get('https://m.douyu.com/' . $roomid);
			}
			break;
		case 'huajiao':
			preg_match('/.*huajiao.com\\/l\\/(\\d+)/is', $url, $matchs);
			if (empty($matchs)) {
			exi('视频地址参数错误或所选来源错误','error','/index/');		
			}
			$roomid = $matchs[1];
			$apiResult = cfc('http')->ihttp_get('http://h.huajiao.com/l/index?liveid=' . $roomid);
			$html = $apiResult['content'];
			preg_match('@"feed":(.*?)"title"@is', $html, $feedInfo);
			if (empty($feedInfo)) {
				exi('获取房间信息失败','error','/index/');	
			}
			$feedInfo = rtrim($feedInfo[1], ',');
			$feedInfo .= '}';
			$feedInfo = json_decode($feedInfo, true);
			$resultArr = array('status' => $feedInfo['paused'] == 'N' ? 1 : 0, 'poster' => $feedInfo['image'], 'hls_url' => !empty($feedInfo['m3u8']) ? $feedInfo['m3u8'] : 'http://qh.cdn.huajiao.com/live_huajiao_v2/' . $feedInfo['sn'] . '/index.m3u8');
			break;
		case 'yizhibo':
			preg_match('/\\/l\\/(.*?).html/is', $url, $matchs);
			if (empty($matchs)) {
				exi('视频地址参数错误或所选来源错误','error','/index/');		
			}
			$roomid = $matchs[1];
			$apiResult = cfc('http')->ihttp_get('http://www.yizhibo.com/l/' . $roomid . '.html');
			$html = $apiResult['content'];
			preg_match('@play_url:"(.*?)",@is', $html, $hls_url);
			preg_match('@covers:"(.*?)",@is', $html, $poster);
			preg_match('@status:(.*?),@is', $html, $status);
			$resultArr = array('status' => $status[1] == 10 ? 1 : 0, 'poster' => $poster[1], 'hls_url' => $hls_url[1]);
			break;
		case 'inke':
			preg_match('/.*id=(\\d+)/is', $url, $matchs);
			if (empty($matchs)) {
				exi('视频地址参数错误或所选来源错误','error','/index/');		
			}
			$roomid = $matchs[1];
			$roomInfo = cfc('http')->ihttp_get('http://webapi.busi.inke.cn/mobile/Get_live_addr?liveid=' . $roomid);
			$roomInfo = json_decode($roomInfo['content'], true);
			if (empty($roomInfo) || !empty($roomInfo['error_code'])) {
				exi('获取房间信息失败','error','/index/');	
			}
			$resultArr = array('status' => $roomInfo['data']['status']);
			if (!empty($roomInfo['data']['status'])) {
				$resultArr['hls_url'] = $roomInfo['data']['live_addr'][0]['hls_stream_addr'];
				$resultArr['hls_url'] = str_replace('rtmp://', 'http://', $resultArr['hls_url']);
				$resultArr['rtmp_url'] = $roomInfo['data']['file'][0];
				$userInfo = cfc('http')->ihttp_get('http://webapi.busi.inke.cn/mobile/user_info?liveid=' . $roomid);
				$userInfo = json_decode($userInfo['content'], true);
				if (!empty($userInfo) && empty($userInfo['error_code']) && !empty($userInfo['data'])) {
					$resultArr['poster'] = $userInfo['data']['image'];
				}
			}
			break;
		case 'shuidi':
			preg_match('/.*view.html\\?.*sn=([0-9A-Z]*)/i', $url, $matchs);
			if (empty($matchs)) {
				exi('视频地址参数错误或所选来源错误','error','/index/');		
			}
			$roomid = $matchs[1];
			$apiResult = cfc('http')->ihttp_get('https://live2.jia.360.cn/public/getInfoAndPlayV2?from=mpc_ipcam_web&sn=' . $roomid);
			$apiResult = json_decode($apiResult['content'], true);
			if (empty($apiResult['publicInfo'])) {
				exi('获取房间信息失败','error','/index/');	
			}
			$resultArr = array('status' => $apiResult['publicInfo']['online'], 'poster' => $apiResult['playInfo']['imageUrl'], 'hls_url' => $apiResult['playInfo']['hls'], 'rtmp_url' => $apiResult['playInfo']['rtmp']);
			break;
		case '360.':
			preg_match('/.*view.html\\?.*sn=([0-9A-Z]*)/i', $url, $matchs);
			if (empty($matchs)) {
				exi('视频地址参数错误或所选来源错误','error','/index/');		
			}
			$roomid = $matchs[1];
			$apiResult = cfc('http')->ihttp_get('https://live2.jia.360.cn/public/getInfoAndPlayV2?from=mpc_ipcam_web&sn=' . $roomid);
			$apiResult = json_decode($apiResult['content'], true);
			if (empty($apiResult['publicInfo'])) {
				exi('获取房间信息失败','error','/index/');	
			}
			$resultArr = array('status' => $apiResult['publicInfo']['online'], 'poster' => $apiResult['playInfo']['imageUrl'], 'hls_url' => $apiResult['playInfo']['hls'], 'rtmp_url' => $apiResult['playInfo']['rtmp']);
			break;
		case 'qlive':
			preg_match('/.*channel\\?id=(\\d+)/is', $url, $matchs);
			if (empty($matchs)) {
				exi('视频地址参数错误或所选来源错误','error','/index/');		
			}
			$roomid = $matchs[1];
			$apiResult = cfc('http')->ihttp_get('https://qlive.163.com/live/square/cameraDetail?deviceId=' . $roomid . '&relateNum=0');
			$apiResult = json_decode($apiResult['content'], true);
			if (empty($apiResult['result']['cameraDetail'])) {
				exi('获取房间信息失败','error','/index/');	
			}
			$resultArr = array('status' => $apiResult['result']['cameraDetail']['canView'], 'poster' => $apiResult['result']['cameraDetail']['coverFileName'], 'hls_url' => 'http://v.smartcamera.163.com/qingguo-public/' . $roomid . '/playlist.m3u8');
			break;
		case 'ys7':
			preg_match('/.*\\?cameraId=(\\d+)/is', $url, $matchs);
			if (empty($matchs)) {
				exi('视频地址参数错误或所选来源错误','error','/index/');		
			}
			$roomid = $matchs[1];
			$apiResult = cfc('http')->ihttp_post('http://square.ys7.com/H5/square/get', array('ids' => $roomid));
			$apiResult = json_decode($apiResult['content'], true);
			if (empty($apiResult['data'])) {
				exi('获取房间信息失败','error','/index/');	
			}
			$apiResult = $apiResult['data'][0];
			$resultArr = array('status' => $apiResult['isPlay'], 'poster' => $apiResult['videoCoverUrl'], 'hls_url' => $apiResult['hlsVideoUrl']);
			break;
		}
		$resultArr['type'] = $type;
		$resultArr['typeName'] = $liveList[$type];
		return $resultArr;
	}
	public function getusersig($identifier, $sdkappid, $private_key){
		$sign=genSig($identifier, $sdkappid, $private_key);
		return $sign;
	}
}
?>